package com.eric.husky.service;

import com.eric.husky.common.utils.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eric.husky.dao.PowerDao;
import com.eric.husky.bean.Power;
import java.util.List;

@Service
public class PowerService {
    @Autowired
	private PowerDao powerDao;

	public Page<Power> list(Power power, Pageable page) {
		Example<Power> example = Example.of(power);
		return powerDao.findAll(example, page);
	}

	public Power get(Integer powerid) {
		return powerDao.findById(powerid).get();
	}

    public List<Power> findList() {
		return powerDao.findAll();
	}

	@Transactional
	public Power save(Power power) {
	    if(power.getPowerid()!=null){
	        Power powerOld = powerDao.findById(power.getPowerid()).orElse(null);
	        if(powerOld!=null){
	            UpdateUtil.copyNullProperties(power,powerOld);
                return powerDao.save(powerOld);
	        }
	    }
	    return powerDao.save(power);
	}

	@Transactional
	public void delete(Integer powerid) {
		powerDao.deleteById(powerid);
	}

}